package com.kzd.btgame.main.popup

import android.content.Context
import android.view.Gravity
import android.view.View
import android.view.animation.Animation
import android.view.animation.TranslateAnimation
import android.widget.*
import com.kzd.btgame.R
import com.kzd.btgame.custom.setOnClickAntiShakeListener
import com.kzd.btgame.entity.CenterConfig
import com.kzd.btgame.entity.MoneySaving
import razerdp.basepopup.BasePopupWindow
import java.lang.StringBuilder


class PaymentBottomPopup(
    context: Context,
    val onMoneySaving: ((view: View, type: Int, moneySaving: MoneySaving) -> Unit)? = null,
    val onCenterConfig: ((view: View, type: Int, centerConfig: CenterConfig) -> Unit)? = null
) : BasePopupWindow(context) {

    init {
        popupGravity = Gravity.BOTTOM
        setOverlayNavigationBar(false)
    }

    override fun onCreateContentView(): View {
        return createPopupById(R.layout.popup_payment_bottom)
    }

    private lateinit var ivPaymentBottomWechat: ImageView

    private lateinit var ivPaymentBottomAli: ImageView

    override fun onViewCreated(contentView: View) {
        super.onViewCreated(contentView)
        contentView.findViewById<ImageView>(R.id.ivPaymentBottomClose).setOnClickAntiShakeListener {
            dismiss()
        }

        contentView.findViewById<TextView>(R.id.tvPaymentBottomSubmit).setOnClickAntiShakeListener { view ->
            dismiss()
            mMoneySaving?.let {
                onMoneySaving?.invoke(view, mType, it)
            }
            mCenterConfig?.let {
                onCenterConfig?.invoke(view, mType, it)
            }
        }

        ivPaymentBottomWechat = contentView.findViewById(R.id.ivPaymentBottomWechat)
        ivPaymentBottomAli = contentView.findViewById(R.id.ivPaymentBottomAli)

        contentView.findViewById<TextView>(R.id.tvPaymentBottomWechat).setOnClickAntiShakeListener {
            mType = 2
            ivPaymentBottomWechat.setImageResource(R.drawable.ic_radio_select)
            ivPaymentBottomAli.setImageResource(R.drawable.ic_radio_default)
        }
        contentView.findViewById<TextView>(R.id.tvPaymentBottomAli).setOnClickAntiShakeListener {
            mType = 1
            ivPaymentBottomAli.setImageResource(R.drawable.ic_radio_select)
            ivPaymentBottomWechat.setImageResource(R.drawable.ic_radio_default)
        }
    }

    override fun onCreateShowAnimation(width: Int, height: Int): Animation {
        return TranslateAnimation(
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 1f,
            Animation.RELATIVE_TO_PARENT, 0f
        ).apply { duration = 300L }
    }

    override fun onCreateDismissAnimation(): Animation {
        return TranslateAnimation(
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 1f
        ).apply { duration = 300L }
    }

    private var mType = 2
    private var mMoneySaving: MoneySaving? = null
    private var mCenterConfig: CenterConfig? = null

    fun fillData(moneySaving: MoneySaving): PaymentBottomPopup {
        this.mType = 2
        this.mMoneySaving = moneySaving
        this.mCenterConfig = null
        contentView.findViewById<TextView>(R.id.tvPaymentBottomSubmit)
            .text = StringBuilder().append("确认支付￥")
            .append(if (moneySaving.first_charge == 1) moneySaving.first_money else moneySaving.pay_money)
        ivPaymentBottomWechat.setImageResource(R.drawable.ic_radio_select)
        ivPaymentBottomAli.setImageResource(R.drawable.ic_radio_default)
        return this
    }

    fun fillData(centerConfig: CenterConfig): PaymentBottomPopup {
        this.mType = 2
        this.mCenterConfig = centerConfig
        this.mMoneySaving = null
        contentView.findViewById<TextView>(R.id.tvPaymentBottomSubmit)
            .text = StringBuilder().append("确认支付￥").append(centerConfig.money)
        ivPaymentBottomWechat.setImageResource(R.drawable.ic_radio_select)
        ivPaymentBottomAli.setImageResource(R.drawable.ic_radio_default)
        return this
    }

}